package imu.g3.dao;


import imu.g3.model.MyProjectUploadFiles;
import imu.g3.util.DataUtil;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;


public class FileDao {
    public List<MyProjectUploadFiles> getAllFiles() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select * from files";
        List<MyProjectUploadFiles> list = new ArrayList<>();
        try {
            conn = new DataUtil().getConnection();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                MyProjectUploadFiles myProjectUploadFiles = new MyProjectUploadFiles();
                myProjectUploadFiles.setFileUploadUser(rs.getString(1));
                myProjectUploadFiles.setFileName(rs.getString(2));
                myProjectUploadFiles.setFileDiscription(rs.getString(3));
                list.add(myProjectUploadFiles);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        return list;
    }

    public int insertFileInfo(MyProjectUploadFiles myProjectUploadFiles) {
        Connection conn = null;
        int res = 0;
        String sql = "insert into files(id,file_name,file_discription,upload_user) values(?,?,?,?)";

        try {
            conn = new DataUtil().getConnection();
            PreparedStatement ptst = conn.prepareStatement(sql);
            ptst.setString(1,myProjectUploadFiles.getId());
            ptst.setString(2,myProjectUploadFiles.getFileName());
            ptst.setString(3,myProjectUploadFiles.getFileDiscription());
            ptst.setString(4,myProjectUploadFiles.getFileUploadUser());
            res = ptst.getUpdateCount();
            ptst.execute();
            ptst.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }
}
